package com.controller;

import com.pojo.Result;
import com.pojo.User;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController {
    UserService us;
    @RequestMapping("/item1")
    public String toMenu1(Model model){
        us = new UserService();
        List<User> list = us.getUserAll();
        model.addAttribute("list",list);
        return "views/menuItem/mUsers";
    }


    @RequestMapping(value="/userController",method = RequestMethod.POST)
    @ResponseBody
    public Result userCon(@RequestBody Map<String,Object> map){
        Result result = null;
        int conid = (int)map.get("conid");
        Map<String,Object> userinfo = (Map)map.get("user");
        User user = new User((String)userinfo.get("username"),(String)userinfo.get("password"));
        us = new UserService();
        switch (conid){
            case 1:
                us.delete(user.getUsername());
            break;
            case 2:
                us.addUser(user);
                break;
        }

        result = Result.success();
        return result;
    }
}
